package com.base
{
	import com.base.data.DataCenter;
	import com.base.event.EventsManager;
	import com.base.properties.PropertiesManmger;
	import com.base.timer.TimerManager;
	import com.base.ui.account.AccountManager;
	import com.base.ui.message.MessageManager;
	import com.base.ui.other.StatusWindow;
	import com.base.ui.sendRule.SendRuleManager;
	
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.containers.TabNavigator;
	import mx.controls.Alert;
	import mx.controls.Button;
	import mx.core.Application;
	import mx.core.FlexGlobals;
	import mx.core.WindowedApplication;
	
	public class Main extends TabNavigator
	{
		
		private static var _instance : Main;
		
		private var _stage:Stage;
		
		private var _statusBtn:Button;
		
		private var msgMag:MessageManager;
		
		private var actMag:AccountManager;
		
		private var ruleMag:SendRuleManager;
		
		public function Main()
		{
			super();
			_instance = this;
			init();
		}
		
		protected function init():void {
			
			/*var curDate:Date = new Date();
			var d:Date = new Date( 2013 , 5 ,30 );
			if( curDate.time >= d.time ) {
				Alert.show("软件已过期,无法使用");
				return;
			}*/
			
			
			addEventListener( Event.ADDED_TO_STAGE , onAdded );
			
			PropertiesManmger.instance.init(function():void {
				
				TimerManager.instance.init(function():void {

					EventsManager.instance.init(function():void{
						
						DataCenter.instance.init(initUI);
						
					})
				})
			});
			
		}
		
		protected function initUI():void {
			addChild( msgMag = new MessageManager());
			addChild( actMag = new AccountManager());
			addChild( ruleMag = new SendRuleManager());
			
			
		}
		
		
		protected function onAdded( e:Event ):void {
			
			removeEventListener( Event.ADDED_TO_STAGE , onAdded );
			_stage = stage;
			(FlexGlobals.topLevelApplication as WindowedApplication).maximize();
		}
		
		
		public static function getMainStage():Stage {
			return _instance._stage;
		}
		
		public static function showStatusBtn():void {
			var app:Application = FlexGlobals.topLevelApplication as Application;
			
			if( !_instance._statusBtn ) { 
				_instance._statusBtn = new Button();
				_instance._statusBtn.label = "查看状态信息";
				_instance._statusBtn.x = (app.width - _instance._statusBtn.width)/2;
				_instance._statusBtn.y = (app.height - _instance._statusBtn.height)/2;
				_instance._statusBtn.addEventListener(MouseEvent.CLICK, function( e:Event ):void {
					StatusWindow.show();
					hideStatusBtn();
				});
			}
			app.addChild( _instance._statusBtn );
		}
		
		public static function hideStatusBtn():void {
			var app:Application = FlexGlobals.topLevelApplication as Application;
			if(app.contains(_instance._statusBtn)) app.removeChild( _instance._statusBtn );
		}
			
	}
}